/*
页面参数
*/
var usermoney=0;//账户金额显示
var currentIndex1=1;//第几页//收入
var getover=0;
var currentIndex2=1;//支出
var payover=0;
var pagesize=20;//加载页面
var noday="";
var changetime="no";//选择时间
var validateLevel=0;
var validateLevelRequest; //验证状态

//var currentdate;//获得当前时间
//var getnow="no";
//var paynow="no";
//var currentIndex3=1;//拒付
//var refuseover=0;
//var refnow="no";



/*
 方法
 */
    //获取个人信息
function getUser(id){
    $.ajax({
        type: "post",
        url: userurl,
        data: {"loginuserId": id,"userId":id},
        datatype: "json",
        success: function (data) {
            if(data.Data.validateLevel){validateLevel=data.Data.validateLevel}
            if(data.Data.validateLevelRequest){validateLevelRequest=data.Data.validateLevelRequest}
            $.cookie("cumulative",data.Data.earnScore+","+data.Data.totalrechare+","+data.Data.expenditure+","+data.Data.totaldesposit);
            $("#rechargenote .allin").html(parseInt(data.Data.earnScore)/100);
            $("#rechargenote .allrecharge").html(parseInt(data.Data.totalrechare)/100);
            $("#withdrawalsnote .allout").html(parseInt(data.Data.expenditure)/100);
            $("#withdrawalsnote .allwithgra").html(parseInt(data.Data.totaldesposit)/100);
        }
    });
}
    //获取个人收入
function getmy(date){
    if(changetime==="yes"){
        $("#waiting1").css("display","block");
        $("#nonews1").css("display","none");
    }
    if(getover===0){
        getover=1;
        $.ajax({
            type:"get",
            datatype:"json",
            url:getmoneyurl+"?userId="+id+"&currentIndex="+currentIndex1+"&pageSize="+pagesize+"&date="+date,
            success:function(d){
                if(!d.Data||d.Data.length===0){
                    getover=1;
                    $("#waiting1").css("display","none");
                    $("#nonews1").css("display","block");
                }
                else{
                    dataList(d,1);
                    if(d.Data.length<pagesize){
                        $("#waiting1").css("display","none");
                        $("#nonews1").css("display","block");
                    }
                    getover=0;
                }
                if(!date){
                    $("#waiting1").css("display","none");
                    $("#nonews1").css("display","block");
                }
            }
        })
    }
}
    //获得个人支出
function paymy(date){
    if(payover===0){
        payover=1;
        $.ajax({
            type:"get",
            datatype:"json",
            url:paymoneyurl+"?userId="+id+"&currentIndex="+currentIndex2+"&pageSize="+pagesize+"&date="+date,
            success:function(d){
                if(!d.Data|| d.Data.length===0){
                    payover=1;
                };
                if(d.Data&& d.Data.length>0){
                    dataList(d,2);
                    if(d.Data.length<pagesize){
                        $("#waiting2").css("display","none");
                        $("#nonews2").css("display","block");
                    }
                    payover=0;
                }else{
                    $("#waiting2").css("display","none");
                    $("#nonews2").css("display","block");
                }
            }
        })
    }
}
    //加载数据列表 公用样式 新类型不做跳转
function dataList(d,typePlace){
    var loadPlace="#pleasewait1";
    if(typePlace===2){loadPlace="#pleasewait2";}
    for(var i=0;i<d.Data.length;i++){
        if(d.Data[i].title){
            var hrefword="javaScript:;";
            var tit=codetoface(d.Data[i].title);
            if(d.Data[i].type===2){
                hrefword="questiondetail.html?qid="+d.Data[i].id+"#fromuser="+id;
            }else if(d.Data[i].type===3){
                hrefword="shareexppage.html?eid="+d.Data[i].id+"#fromuser="+id;
            }
            var tim=/[^\d{4}]\d{2}\-\d{2}/.exec(d.Data[i].dateTime)[0].match(/\d{2}\-\d{2}/)[0];
            $("<div class='box getmoneybox'><a href='"+hrefword+"'><div class='daymoney'>"+tim+"</div><div class='title'>"+tit+"</div>  <div class='allmoney'>"+parseFloat(d.Data[i].scoreCountAll)/100+"元</div></a></div>").insertBefore(loadPlace);
        }
    }
}
    //提现判断
function gotest(){
    if($.cookie("MYID")){
        if(usermoney<100){
            $("#nokeywordalert").css("display","block");
            $("#nokeywordalert .part1").css("display","block");
            $("#nokeywordalert .part2").css("display","none");
        }else{
            $("#nokeywordalert").css("display","block");
            $("#nokeywordalert .part1").css("display","none");//钱不够
            $("#nokeywordalert .part2").css("display","none");//认证
            $("#nokeywordalert .part3").css("display","none");//审核中
            $("#nokeywordalert .part4").css("display","none");//未通过
            if(!validateLevelRequest){
                $("#nokeywordalert .part2").css("display","block");//认证
            }else if(validateLevelRequest===0){  //提现
                location.href="withdrawals.html";
            } else if(validateLevelRequest===1){ //审核中
                $("#nokeywordalert .part3").css("display","block");//审核中
            }else if(validateLevelRequest===2){  //未通过
                $("#nokeywordalert .part4").css("display","block");//未通过
            }
        }
    }else{
        location.href="http://www.pulu.com/weixin/login.html";
    }
}
    //判断是否为PC
function IsPC() {
    var userAgentInfo = navigator.userAgent;
    var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");
    var flag = true;
    for (var v = 0; v < Agents.length; v++){
        if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }
    }
    return flag;
}
function getOs(){
    //var OsObject = "";
    if(navigator.userAgent.indexOf("MSIE")>0) {return "MSIE";}
    if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){return "Firefox";}
    if(isSafari=navigator.userAgent.indexOf("Safari")>0) {return "Safari";}
    if(isCamino=navigator.userAgent.indexOf("Camino")>0){return "Camino";}
    if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0){return "Gecko";}
}
    //关闭弹框
function closeModel(){$("#nokeywordalert").css("display","none");}
    //去认证
function goAuthenticate(){location.href="authenticateUser.html";}



/*
 获取初始参数
 */
    //获取用户信息
if(!$.cookie("MYID")){
    if(/userid=[0-9a-z]+/.test(location.href)){
        var splituserid=(location.href).match(/userid=[0-9a-z]+/)[0].split("=")[1];
        if((location.href)=="http://www.pulu.com/weixin/scoredetail.html?userid="+splituserid){
            id=splituserid;
            getUser(id);
        }
        else{location.href="http://www.pulu.com/weixin/login.html";}
    }else{location.href="http://www.pulu.com/weixin/login.html";}
}else{
    id=$.cookie("MYID");
    getUser(id);
}
    //获取余额
$.ajax({
    type:"get",
    datatype:"json",
    url:lastscoreurl+id,
    success:function(d){
        usermoney = Math.round(d.Data)/100;
        $("#lastmoneyword").html(usermoney+"元");
    }
});

if(IsPC()){
    $("#bottomarr").css("display","none");
    $("#bottomarr2").css("display","none");
    $("#daywordpc").css("display","none");
    $("#daywordpc2").css("display","none");
    $("#dayword").css("display","none");
    $("#dayword2").css("display","none");
    $("div.box>div.datebox").css("height","20px");
    $("#getmoneyinput").css({"left":"115px","opacity":"1","width":"auto","fontSize":"15px","textAlign":"center","height":"20px","lineHeight":"20px","top":"16px"});
    $("#paymoneyinput").css({"left":"90px","opacity":"1","width":"auto","fontSize":"15px","textAlign":"center","height":"20px","lineHeight":"20px","top":"16px"});
    if(getOs()==="Firefox"||getOs()==="MSIE"){
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {month = "0" + month;}
        if (strDate >= 0 && strDate <= 9) {strDate = "0" + strDate;}
        console.log("浏览器不支持input date控件");
        $("#getmoneyinput").val(year+"/"+month+"/"+strDate);
        $("#paymoneyinput").val(year+"/"+month+"/"+strDate);
        $("#getmoneyinput").css("left","85px");
        $("#paymoneyinput").css("left","50px");
    }
}else{
    $("#daywordpc").css("display","none");
    $("#daywordpc2").css("display","none");
}

getmy(noday);
paymy(noday);

/*
 点击
 */
    //页面切换
$("#gbtn").click(function(){
    $("#gbtn").attr("class","heads_up_item is_active");
    $("#pbtn").attr("class","heads_up_item");
    $("#getmoney").css("display","block");
    $("#paymoney").css("display","none");
    //累计
    $("#rechargenote").css("display","block");
    $("#withdrawalsnote").css("display","none");
});

$("#pbtn").click(function(){
    $("#pbtn").attr("class","heads_up_item is_active");
    $("#gbtn").attr("class","heads_up_item");
    $("#paymoney").css("display","block");
    $("#getmoney").css("display","none");
    //累计
    $("#rechargenote").css("display","none");
    $("#withdrawalsnote").css("display","block")
});

document.getElementsByClassName("mweb_modal_back_button")[0].onclick=function(){
    if(document.referrer){history.go(-1);}
    else{location.href="http://www.pulu.com/weixin/pulu.html";}
};

$("#closealertbtn").click(function(){$("#nokeywordalert").css("display","none")});

$("#closePart2").click(function(){$("#nokeywordalert").css("display","none")});

$("#Rechargebtn").click(function(){
    if($.cookie("MYID")){
        if(is_weixn()){
            location.href="http://www.pulu.com/weixin/recharge.html";
        }else{
            location.href="http://www.pulu.com/weixin/rechargepc.html";
        }
    }
    else{location.href="http://www.pulu.com/weixin/login.html";}
});



/*
  绑定监听
  */
$('#getmoneyinput').bind('input propertychange', function() {
    changetime="yes";
    var date=$(this).val();
    if(getOs()==="Firefox"||getOs()==="MSIE"){
        date=date.replace("/","-");
    }
    $(".datebox span#dayword").html($(this).val().match(/\d+$/)[0]+"日收入");
    //时间清空
    $("#getmoneyplace div.getmoneybox").remove();
    $("#waiting1").css("display","block");
    $("#nonews1").css("display","none");
    getover=0;
    getmy(date);
});
$('#paymoneyinput').bind('input propertychange', function() {
    changetime="yes";
    var date=$(this).val();
    if(getOs()==="Firefox"||getOs()==="MSIE"){
        date=date.replace("/","-");
    }
    $(".datebox span#dayword2").html($(this).val().match(/\d+$/)[0]+"日支出");
    //时间清空
    $("#paymoneyplace div.getmoneybox").remove();
    $("#waiting2").css("display","block");
    $("#nonews2").css("display","none");
    getover=0;
    paymy(date);
});
//页面滚动
$("#getmoneyplace").scroll(function(){
    var mainheight=$(".detialplace").height();
    var windowheight=$(window).height();
    var chajuli=$(".detialplace").offset().top - $(window).scrollTop();
    var cz=windowheight-mainheight+10;
    if(chajuli<cz){
        //判断哪个页面滑动
        if($("#getmoney").css("display")==="block"&&changetime==="yes"){
            currentIndex1++;
            var date1=$('#getmoneyinput').val();
            getmy(date1);
        }else if($("#paymoney").css("display")==="block"){
            currentIndex2++;
            paymy();
        }
    }
});
$("#paymoneyplace").scroll(function(){
    var mainheight=$(".detialplace").height();
    var windowheight=$(window).height();
    var chajuli=$(".detialplace").offset().top - $(window).scrollTop();
    var cz=windowheight-mainheight+10;
    if(chajuli<cz){
        //判断哪个页面滑动
        if($("#getmoney").css("display")==="block"){
            currentIndex1++;
            var date1=$('#getmoneyinput').val();
            getmy(date1);
        }else if($("#paymoney").css("display")==="block"){
            currentIndex2++;
            paymy();
        }
    }
});

//$().ready(

    //refuse()
//);

//if($.cookie("cumulative")){
//    var momeynote=$.cookie("cumulative").split(",");
//    $("#rechargenote .allin").html(parseInt(momeynote[0])/100);
//    $("#rechargenote .allrecharge").html(parseInt(momeynote[1])/100);
//    $("#withdrawalsnote .allout").html(parseInt(momeynote[2])/100);
//    $("#withdrawalsnote .allwithgra").html(parseInt(momeynote[3])/100);
//}
//(function(){
//    var date = new Date();
//    var seperator1 = "-";
//    var year = date.getFullYear();
//    var month = date.getMonth() + 1;
//    var strDate = date.getDate();
//    if (month >= 1 && month <= 9) {
//        month = "0" + month;
//    }
//    if (strDate >= 0 && strDate <= 9) {
//        strDate = "0" + strDate;
//    }
//    //$(".datebox span#dayword").html(strDate+"日");
//    $(".datebox span#dayword").html("时间");
//    $(".datebox span#dayword2").html("时间");
//    //currentdate = year + seperator1 + month + seperator1 + strDate;
//})();
//console.log(currentdate);
//$('#getmoneyinput').val(currentdate);//调整时间